﻿<%@ WebHandler Language="C#" Class="UploadHandler" %>

using System;
using System.Web;

public class UploadHandler : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        context.Response.Charset = "utf-8";
        int Class = Convert.ToInt32(context.Request["id"].ToString());
        HttpPostedFile file = context.Request.Files["Filedata"];
        UserHelper u = new UserHelper();
        string fileName = u.GetImgNameMaxNum(Class);
        string uploadPath = HttpContext.Current.Server.MapPath(@"~/img/Album/" + Class + "/");
        if (file != null)
        {
            if (!System.IO.Directory.Exists(uploadPath))
            {
                System.IO.Directory.CreateDirectory(uploadPath);
            }
            string[] sArray = file.FileName.Split('.');
            string varname = sArray[sArray.Length - 1];
            file.SaveAs(uploadPath + "temp\\" + fileName +"."+ varname);
            Compressd.Compress(uploadPath + "temp\\"+fileName+"."+ varname, uploadPath + fileName+".jpeg", 40);
            ImgCompression imgy = new ImgCompression(uploadPath + "temp\\" + fileName +"."+ varname);//压缩缩略图
            imgy.ReducedImage(100, 90, uploadPath + "mini\\" + fileName+".jpeg");//设置缩略的尺寸
            string delectPath = uploadPath + "temp\\" + fileName +"."+ varname;
            if (System.IO.File.Exists(delectPath))//先判断文件是否存在，再执行操作
                System.IO.File.Delete(delectPath);
            context.Response.Write("1");
        }
        else
        {
            context.Response.Write("0");
        }  
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}